package com.briup.corejava.day14;

import java.util.*;

/**
 * 斗地主发牌
 */
public class DdzTest {
    // 使用静态变量给点数设置权重
    public static final Map<String,Integer> map;
    static{
        map = new HashMap<>();
        map.put("3",1);
        map.put("4",2);
        map.put("5",3);
        map.put("6",4);
        map.put("7",5);
        map.put("8",6);
        map.put("9",7);
        map.put("10",8);
        map.put("J",9);
        map.put("Q",10);
        map.put("K",11);
        map.put("A",12);
        map.put("2",13);
    }
    public static void main(String[] args) {
        // 0、准备一副牌
        List<Card> list = new ArrayList<>();
        // 1、准备花色和点数的固定的数组
        // 点数
        String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        // 花色
        Card.Color[] colors = Card.Color.values();
        // 根据花色和点数，使用双循环完成组牌，52张
        for(String s:nums){
            for(Card.Color c : colors){
                Card card = new Card(c, s, map.get(s));
                list.add(card);
            }
        }
        // 大小王单独创建
        Card c1 = new Card(null, "joker", 14);
        Card c2 = new Card(null, "JOKER", 15);
        // 要将两张牌拼接到牌库中
        Collections.addAll(list,c1,c2);
        // 洗牌
        Collections.shuffle(list);
        // 输出得到的牌库
        System.out.println(list);
        // 发牌
        // 准备三个玩家，只需要提供名字，默认角色是农民，默认手里的牌是空的
        Player p1 = new Player("张三");
        Player p2 = new Player("李四");
        Player p3 = new Player("王武");
        // 把三张底牌先取出来
        List<Card> dipai = list.subList(51, 54);
        // 展示底牌
        System.out.println("展示底牌："+dipai);
        // 发牌
        for(int i=0;i<list.size();i++){
            if(i%3 == 0){
                p1.getCardList().add(list.get(i));
            }else if(i%3==1){
                p2.getCardList().add(list.get(i));
            }else{
                p3.getCardList().add(list.get(i));
            }
        }
        // 确定谁是地主，简单化，取0-2的随机数来确定，给底牌的
        Random random = new Random();
        int i = random.nextInt(3);
        // 确认地主，变换身份，并且将底牌拿到手中
        if(i==0){
            p1.setRole("地主");
            p1.getCardList().addAll(dipai);
        }else if(i==1){
            p2.setRole("地主");
            p2.getCardList().addAll(dipai);
        }else {
            p3.setRole("地主");
            p3.getCardList().addAll(dipai);
        }
        // 提供一个比较器，使用sort方法来进行排序
        Comparator<Card> comparator = new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                // 先按点数排，点数排完按花色
                if(o1.getNum().equals(o2.getNum())){
                    return o1.getColor().getH()-o2.getColor().getH();
                }
                return o1.getH() - o2.getH();
            }
        };
        p1.getCardList().sort(comparator);
        Collections.sort(p2.getCardList(),comparator);
        Collections.sort(p3.getCardList(),comparator);

        // 输出三个玩家的手牌
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
    }
}
class Card{
    private Color color; // 花色
    private String num; // 点数
    private Integer h; // 权重
    enum Color{
        SPADES("♠️",4),
        HEART("♥️",3),
        CLUB("♣️",2),
        DIAMONDS("♦️",1);
        private String desc;
        private Integer h;

        Color(String desc, Integer h) {
            this.desc = desc;
            this.h = h;
        }

        public String getDesc() {
            return desc;
        }

        public Integer getH() {
            return h;
        }
    }

    public Card(Color color, String num, Integer h) {
        this.color = color;
        this.num = num;
        this.h = h;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public Integer getH() {
        return h;
    }

    public void setH(Integer h) {
        this.h = h;
    }

    @Override
    public String toString() {
        return color==null?num:color.getDesc()+num;
    }
}
class Player{
    private String name;
    private String role;
    private List<Card> cardList;
    // 只提供单参的构造器
    public Player(String name){
        this.name = name;
        role = "农民";
        cardList = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<Card> getCardList() {
        return cardList;
    }

    public void setCardList(List<Card> cardList) {
        this.cardList = cardList;
    }

    @Override
    public String toString() {
        return name+" ["+role+"] \n手牌为："+cardList;
    }
}
